package event;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;
import javax.swing.JLabel;

class CanEvent extends MouseMotionAdapter implements MouseListener{
	JLabel label;
	Canvas can;

	public CanEvent(JLabel l, Canvas c) {
		this.label = l;
		this.can = c;
	}

	@Override
	public void mouseMoved(MouseEvent ev) {
		int x = ev.getX();
		int y = ev.getX();
		label.setText("x=" + x + ",y=" + y);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent e) {  // 눌렀을때
		// TODO Auto-generated method stub
		Graphics g = can.getGraphics();
		int x = e.getX();
		int y = e.getY();
		int size = 100;
		g.drawOval(x-50, y-50, size, size);
	}

	@Override
	public void mouseReleased(MouseEvent e) {   // 띄었을경우
		// TODO Auto-generated method stub
		
	}
}

public class MyFrame04 extends JFrame {

	JLabel lbl = new JLabel("현재좌표");
	private Canvas can = new Canvas();

	public MyFrame04() {
		this.setBounds(100, 100, 500, 400);
		this.setVisible(true);
		this.add(can);

		can.setBackground(Color.red);
		this.add(can);
		this.add(lbl, BorderLayout.NORTH);

		CanEvent ce = new CanEvent(lbl , can);
		can.addMouseMotionListener(ce);
		can.addMouseListener(ce);
	}

	public static void main(String[] args) {
		new MyFrame04();

	}

}
